using System.ComponentModel;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.VisualTree;
using JMClient.ViewModels;
using Material.Styles.Controls;

namespace JMClient.Views
{
    public partial class ComponentsList : UserControl
    {
        private WrapPanel? _panel;
        public ComponentsList()
        {
            InitializeComponent();
            // 在控件附加到视觉树之后再取 ItemsPanelRoot
            this.LayoutUpdated += (_, _) =>
            {
                // 1. 通过 x:Name 拿到 ItemsControl 实例
                var ic = this.FindControl<ItemsControl>("deviceList");

                // 2. ItemsPanelRoot 就是 WrapPanel 的实例
                _panel = ic.ItemsPanelRoot as WrapPanel;

                if (_panel != null)
                    _panel.SizeChanged += OnPanelSizeChanged;
            };
           
        }

        

        // 使用 SizeChangedEventArgs 拿到新的尺寸
        private void OnPanelSizeChanged(object? sender, SizeChangedEventArgs e)
        {
            var width = e.NewSize.Width;

            // 根据断点计算列数
            int cols = width < 600   ? 1
                : width < 900   ? 2
                : width < 1200  ? 3
                : 4;
            if (devicePanel is null)
            {
                
            }
            else
            {
               // WrapPanel 的水平间距用 LineSpacing
                           var spacing = devicePanel.LineSpacing;
               
                           // 重新计算并赋值 ItemWidth
                           devicePanel.ItemWidth = (width - (cols - 1) * spacing) / cols; 
            }
            
        }

        private async void OnCardClicked(object? sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            // if (sender is ContentPresenter cp
            //     && cp.DataContext is DeviceEntityViewModel vm)
            // {
            //     vm.OpenCommand.Execute();
            // }
            /*if (sender is ContentPresenter cp
                && cp.DataContext is DeviceEntityViewModel vm)
            {
                // 如果已经附着到视觉树，则立即执行
                if (cp.IsAttachedToVisualTree())
                {
                    vm.OpenCommand.Execute();
                    return;
                }

                // 否则订阅 AttachedToVisualTree，一次性处理后取消订阅
                void Handler(object? s, VisualTreeAttachmentEventArgs args)
                {
                    cp.AttachedToVisualTree -= Handler;
                    // 保证在 UI 线程执行
                    Avalonia.Threading.Dispatcher.UIThread.Post(() => vm.OpenCommand.Execute());
                }

                cp.AttachedToVisualTree += Handler;
            }*/
            // var List = sender as ListBox;
            // if (List.SelectedItem is DeviceEntityViewModel vm)
            // {
            //     var t = List.SelectedItem as DeviceEntityViewModel;
            //      
            //      
            // }
            
        }

        private void InitializeComponent() => AvaloniaXamlLoader.Load(this);
    }
}